<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/14
 * Time: 15:38
 */

namespace App\Service;

use App\Models\Advert;

class AdvertService
{
    /**
     * 获取广告列表
     * @param array $params
     * @param string $orderBy
     * @param string $sort
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getList($params = [],$orderBy = 'id',$sort = 'DESC',$type = 1)
    {
        $where = [];
//        $posterModel = new Poster();
        $list = Advert::where($where)->orderBy($orderBy,$sort)->get()->toArray();
        if($type == 2){
            foreach($list as $k => $v){
                $list[$k]['img_src'] = $v['img_src'] ?  env('APP_URL').$v['img_src'] : '';
            }
        }

        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$list]);
    }



    /**
     * 通过ID获取广告信息
     * @param int $id
     * @return array
     */
    public static function getAdvertById($id = 0)
    {
        $info = Advert::where('id', $id)->first(['id', 'img_src', 'appid', 'page_path', 'order', 'created_at', 'updated_at']);
        $info = $info ? $info->toArray() : [];
        return Response()->json(['code' => 200, 'msg' => '获取成功', 'data' => $info]);
    }

    /**
     * 添加
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function add($params = [])
    {
        try {
            $info = Advert::create(['img_src'=>trim($params['img_src']),'appid'=>trim($params['appid']),'page_path'=>trim($params['page_path']),'order'=>intval($params['order'])]);
            if(!$info){
                throw new \Exception('新增广告失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'添加广告成功']);
    }

    /**
     * 修改
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function edit($params = [])
    {
        try{
            $info = Advert::where('id',intval($params['id']))->first();
            if(!$info){
                throw new \Exception('该广告不存在');
            }
            $info->img_src      = trim($params['img_src']);
            $info->appid        = trim($params['appid']);
            $info->page_path    = trim($params['page_path']);
            $info->order        = intval($params['order']);
            $save = $info->save();
            if(!$save){
                throw new \Exception('修改失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'操作成功']);
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public static function destroy($id)
    {
        $idArr = is_array($id) ? $id : [$id];
        if(!$idArr){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的广告']);
        }
        $del = Advert::destroy($idArr);
        if($del){
            return Response()->json(['code'=>200,'msg'=>'删除成功']);
        }
        return Response()->json(['code'=>201,'msg'=>'删除失败']);
    }

}
